<?php
namespace app\Http\Controllers\admin_system;

use DB;
use App\Http\Controllers\Controller;
use App\Module\EntManageuser;
use Illuminate\Http\Request;
use \app\Module;

class UserController extends Controller
{
    /**
     * 查询所有记录
     */
    public function index()
    {
        $EntManageuserAll = DB::table('manageuser')->select('UserID', 'UserName', 'RealName', 'Tel', 'Email', 'ReleaseDate')
            ->where('DeleteMark', '1')
            ->orderBy('ReleaseDate', 'desc')
            ->get();
        return view('admin_system/user/userList', ['Manageuser' => $EntManageuserAll]);
    }

    public function edit($id)
    {
        $EntManageuser = EntManageuser::where('UserID', '=', $id)->where('DeleteMark', '=', '1')->first();
        if(!$EntManageuser){
            $EntManageuser = new \stdClass();
            $user = EntManageuser::first()->toArray();
            foreach($user as $key => $v){
                $EntManageuser->$key = "";
            }
        }
        return view('admin_system/user/editUser', ['Manageuser' => $EntManageuser]);
    }


    /**
     * 管理员添加
     *
     * @param Request $request            
     */
    public function Insert(Request $request)
    {
        $this->validate($request, [
            'txtUserName' => 'required',
            'txtUserPwd' => 'required'
        ]);
        $EntManageuser = new EntManageuser();
        $EntManageuser->UserName = $request->get('txtUserName');
        $EntManageuser->UserPwd = md5($request->get('txtUserPwd'));
        $EntManageuser->RealName = $request->get('txtRealName');
        $EntManageuser->Tel = $request->get('txtTel');
        $EntManageuser->Email = $request->get('txtEmail');
        $EntManageuser->ReleaseDate = date('Y-m-d H:i:s', time());
        $result = $EntManageuser->save();
        $result = $result?"ok":"no";
        return view('admin_system/user/addUser')->with('result', $result);
    }

    /**
     * 管理员信息修改
     *
     * @param Request $request            
     * @param unknown $id            
     */
    public function update_user(Request $request)
    {

        $userId = $request->get('userId');
        $userPwd= $request->get('txtUserPwd');
        $userPwd2=$request->get('txtUserPwd2');
        if($userPwd != $userPwd2){
            return redirect()->back()->withErrors('密码不一致');
        }
        $date = date('Y-m-d H:i:s', time());
        $this->validate($request, ['txtUserName' => 'required']);
        if(empty($userId)){
            if(empty($userPwd)){
                return redirect()->back()->withErrors('密码不能为空');
            }
            $EntManageuser = new EntManageuser();
            $EntManageuser->AddDate = $date;
        }else{
            $EntManageuser = EntManageuser::where('UserID',$userId)->first();
        }

        if(!empty($userPwd)){
            $EntManageuser->UserPwd = md5($userPwd);
        }

        $EntManageuser->UserName = $request->get('txtUserName');
        $EntManageuser->RealName = $request->get('txtRealName');
        $EntManageuser->Tel      = $request->get('txtTel');
        $EntManageuser->Email    = $request->get('txtEmail');
        $EntManageuser->ReleaseDate = $date;
        $EntManageuser->save();

        return redirect('/admin_system/user/userList');
    }



    /** ===== 用户列表 ===* */
    public function user_list()
    {
        $UserPage = EntManageuser::where('DeleteMark', '=', '1')->orderBy('ReleaseDate', 'desc')->paginate(3);
        return view('admin_system/user/userList', ['Manageuser' => $UserPage]);
    }


    /*===删除管理员===*/
    public function Delete(Request $request)
    {
       $result = '';
        try {
            if ($request->ajax()) {
                $IDList = $request->json('IDList');
                if (is_numeric($IDList)) {
                    EntManageuser::where('UserID', $IDList)->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                } else {
                    EntManageuser::whereIn('UserID',explode(',', $IDList))->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }
}
?>